const app = getApp();
Page({
  data: {
    name: "",
    building: "",
    room: "",
    number: null,
    department: "",
    hobby: "",
    wakeTime: "7:00",
    sleepTime: "23:00"
  },



  onReady: function(){
    let that = this;
    wx.request({
      url: 'https://dorm.dormassistant.wang:8080/userInformation/queryUserInformationById',
      data:{
        userId: wx.getStorageSync('openid')
      },
      method:"GET",
      success: function(res){
        that.setData({
          name: res.data.user.name,
          building: res.data.user.building,
          room: res.data.user.room,
          number: res.data.user.number,
          department: res.data.user.department,
          hobby: res.data.user.hobby,
          wakeTime: res.data.user.wakeTime.split("T")[1].split(":")[0] + ":" + res.data.user.wakeTime.split("T")[1].split(":")[1],
          sleepTime: res.data.user.sleepTime.split("T")[1].split(":")[0] + ":" + res.data.user.sleepTime.split("T")[1].split(":")[1]
        });
      }
    });
  },



  formsubmit: function (e) {
    var that = this;

    var userId = app.globalData.userId;
    var name = e.detail.value.name;
    var building = e.detail.value.building;
    var room = e.detail.value.room;
    var number = e.detail.value.number;
    var department = e.detail.value.department;
    var nickName = app.globalData.userInfo.nickName;
    var gender = app.globalData.userInfo.gender;
    var hobby = e.detail.value.hobby;
    var sleepTime = e.detail.value.sleepTime;
    var wakeTime = e.detail.value.wakeTime;
    var avatarUrl = app.globalData.userInfo.avatarUrl;


    wx.request({
      url: 'https://dorm.dormassistant.wang:8080/userInformation/modifyUserInformation',
      data: {
        name : name,
        userId: wx.getStorageSync('openid'),
        building: building,
        room : room,
        number: number,
        department: department,
        nickName : nickName,
        gender: gender,
        hobby: hobby,
        sleepTime: sleepTime,
        wakeTime: wakeTime,
        avatarUrl: avatarUrl
      },
      method:"POST",
      header: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=utf8'
      },
      success: function (res) {
        var toastText = '修改信息成功';
        wx.showToast({
          title: toastText,
          icon: '',
          duration: 2000
        });
        wx.navigateBack({
          url: '../home/home',
        })
      },
      fail: function(res){
        var toastText = '提交失败' + res.data.errMsg;
        wx.showToast({
          title: toastText,
          icon: '',
          duration: 2000
        });
      }
    });
  },


  sleepTime: function (e) {
    this.setData({
      sleepTime: e.detail.value
    })
  },
  wakeTime: function (e) {
    this.setData({
      wakeTime: e.detail.value
    })
  }
  
});